// Chart.cs
// 
// Copyright (C) 2008 Jean-André Santoni
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System;
using System.Diagnostics;
using System.Collections.Specialized;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Astrognome
{
	[Serializable()]
	
	public class Chart : ISerializable
	{
		private string filename;
		private bool saved;
		private string name;
		private DateTime datetime;
		private Location location;
		private bool dst;
		private double timezone;
		
		public string   FileName { get { return filename; } set { filename = value; } }
		public bool     Saved    { get { return saved;    } set { saved = value;    } }
		public string   Name     { get { return name;     } set { name = value;     } }
		public DateTime DateTime { get { return datetime; } set { datetime = value; } }
		public Location Location { get { return location; } set { location = value; } }
		public bool     DST      { get { return dst;      } set { dst = value;      } }
		public double   TimeZone { get { return timezone; } set { timezone = value; } }
		
		public string AstrologOutput
		{
			get
			{				
				//Environment.SetEnvironmentVariable("ASTR", "./astrolog/ephe");
				
				Process p = new Process();
				p.StartInfo.UseShellExecute = true;
				p.StartInfo.RedirectStandardOutput = false;
				p.StartInfo.CreateNoWindow = true;
				p.StartInfo.FileName  = "astrolog -b -qb ";
				p.StartInfo.FileName += DateTime.Month + " ";
				p.StartInfo.FileName += DateTime.Day + " ";
				p.StartInfo.FileName += DateTime.Year + " ";
				p.StartInfo.FileName += DateTime.Hour + ":" + DateTime.Minute + " ";
				p.StartInfo.FileName += Convert.ToInt32(DST) + " ";
				p.StartInfo.FileName += TimeZone + " ";
				p.StartInfo.FileName += Location.Longitude + " ";
				p.StartInfo.FileName += Location.Latitude + " ";
				p.StartInfo.FileName += "> /tmp/chart.txt";

				p.Start();
				p.WaitForExit();
				TextReader txt = new StreamReader("/tmp/chart.txt");
				return txt.ReadToEnd();
				
//				Process p = new Process();
//				p.StartInfo.UseShellExecute = false;
//				p.StartInfo.RedirectStandardOutput = true;
//				p.StartInfo.CreateNoWindow = true;
//				p.StartInfo.WorkingDirectory = "./astrolog/";
//				p.StartInfo.FileName  = "astrolog";
//				p.StartInfo.Arguments = "-b -qb ";
//				p.StartInfo.Arguments += DateTime.Month + " ";
//				p.StartInfo.Arguments += DateTime.Day + " ";
//				p.StartInfo.Arguments += DateTime.Year + " ";
//				p.StartInfo.Arguments += DateTime.Hour + ":" + DateTime.Minute + " ";
//				p.StartInfo.Arguments += Convert.ToInt32(DST) + " ";
//				p.StartInfo.Arguments += "-1 ";
//				p.StartInfo.Arguments += Location.Longitude + " ";
//				p.StartInfo.Arguments += Location.Latitude;
//
//				p.Start();	
//				p.WaitForExit();
//				return p.StandardOutput.ReadToEnd();
			}
		}
		
		public Chart ()
		{
			FileName = "";
			Saved =    false;
			Name =     "Nobody";
			DateTime = new DateTime();
			Location = new Location();
			DST =      false;
			TimeZone = 0;
		}
		
		public Chart (string name, DateTime datetime, Location location, bool dst, double timezone)
		{
			FileName = "";
			Saved =    false;
			Name =     name;
			DateTime = datetime;
			Location = location;
			DST =      dst;
			TimeZone = timezone;
		}
		
		public Chart (SerializationInfo info, StreamingContext ctx)
		{
			Saved = true;
			FileName = (string)   info.GetValue("FileName", typeof(string));
			Name =     (string)   info.GetValue("Name",     typeof(string));
			DateTime = (DateTime) info.GetValue("DateTime", typeof(DateTime));
			Location = (Location) info.GetValue("Location", typeof(Location));
			DST =      (bool)     info.GetValue("DST",      typeof(bool));
			TimeZone = (double)   info.GetValue("TimeZone", typeof(double));
		}

		public void GetObjectData (SerializationInfo info, StreamingContext ctx)
		{
			info.AddValue("FileName", FileName);
			info.AddValue("Name",     Name);
			info.AddValue("DateTime", DateTime);
			info.AddValue("Location", Location);
			info.AddValue("DST",      DST);
			info.AddValue("TimeZone", TimeZone);
		}
		
		public override string ToString ()
		{
			return Name + " born on " + DateTime + " in " + Location;
		}

	}
}
